/*
 * @lc app=leetcode id=135 lang=cpp
 *
 * [135] Candy
 */
class Solution {
public:
    int candy(vector<int>& ratings) {
        int len = ratings.size();
        if (len <= 1) {
            return len;
        }

        vector<int> score(len, 1);

        for (int i=1; i<len; i++) {
            if (ratings[i] > ratings[i-1]) {
                score[i] = score[i-1]+1;
            }
        }

        for (int i=len-1; i>0; i--) {
            if (ratings[i-1] > ratings[i]) {
                score[i-1] = std::max(score[i]+1, score[i-1]);
            }
        }

        int sum = 0;
        for (auto s : score) {
            sum += s;
        }
        
        return sum;
    }
};

